package game.controller;

import game.model.*;
import java.util.ArrayList;
import java.util.List;

public class Board {
	
	private List <Player> players;
	private List <Room> rooms;
	
	private Board ()
	{
		
	}
	
	private static class BoardHolder
	{
		private static final Board INSTANCE = new Board ();
	}
	
	public static Board getInstance ()
	{
		return BoardHolder.INSTANCE;
	}
	
	void initBoardfromXml (xmlHandler.jaxbGenerated.Clue xmlClueNode)
	{
		rooms = new ArrayList <Room> ();
		
		for (Room.RoomName roomName : Room.RoomName.values())
			rooms.add(new Room (roomName.getRoomName()));
		
		Clue.getInstance().setMurderRoom(new Room (xmlClueNode.getMurderRoom().value()));
		Clue.getInstance().setMurderer(new Suspect (xmlClueNode.getMurderer().value()));
		Clue.getInstance().setMurderWeapon(new Weapon (xmlClueNode.getMurderWeapon().value()));
				
		players = new ArrayList <Player> ();
		
		for (xmlHandler.jaxbGenerated.Player playerFromXml : xmlClueNode.getPlayers().getPlayer())
		{
			Player player = new Player ();
			
			if (playerFromXml != null)
				player.setName(playerFromXml.getName());
			
			if (playerFromXml.getType() != null)
				player.setPlayerType(playerFromXml.getType().value());
			
			if (playerFromXml.getCharacter() != null)
				player.setSuspect(new Suspect (playerFromXml.getCharacter().value()));
			
			Deck deck = new Deck ();
			
			for (xmlHandler.jaxbGenerated.Card cardFromXml : playerFromXml.getCards().getCard())
			{
				Card card = new Card ();
				
				if (cardFromXml.getType() != null)
					card.setCardType(cardFromXml.getType().value());
				
				if (cardFromXml.getRoom() != null)
					card.setRoom(new Room (cardFromXml.getRoom().value()));
				
				if (cardFromXml.getSuspect() != null)
					card.setSuspect(new Suspect (cardFromXml.getSuspect().value()));
				
				if (cardFromXml.getWeapon() != null)
					card.setWeapon(new Weapon (cardFromXml.getWeapon().value()));

				deck.addCardToDeck(card);
			}
			
			player.setCards(deck);
			
			players.add(player);
		}
	}
	
	public void print ()
	{
		System.out.println("Clue:");
		System.out.println("SuspectName: " + Clue.getInstance().getMurderer().getSuspectName());
		System.out.println("MurderRoom: " + Clue.getInstance().getMurderRoom().getRoomName());
		System.out.println("MurderWeapon: " + Clue.getInstance().getMurderWeapon().getWeaponName());
		
		int i = 1;
		for (Player player : players)
		{
			int j = 1;
			System.out.println("Player"+i);
			if (player != null) 
			{
				System.out.println("name: " + player.getName());
				System.out.println("type: " + player.getPlayerType());
					
				if (player.getSuspect() != null)
					System.out.println("suspect: " + player.getSuspect().getSuspectName());
					
				if (player.getCards() != null)
				{
					if (player.getCards().getCards() != null)
					{
						for (Card card : player.getCards().getCards())
						{
							System.out.println("card"+j);
							System.out.println("CardType: " + card.getCardType());
								
							if (card.getRoom() != null)
								System.out.println("room: " + card.getRoom().getRoomName());
								
							if (card.getSuspect() != null)
								System.out.println(card.getSuspect().getSuspectName());
								
							if (card.getWeapon() != null)
								System.out.println(card.getWeapon().getWeaponName());
							
							j++;
						}
					}
				}
			}
			i++;
		}
	}
}
